<?php
namespace app\common\logic;

use think\Model;
use app\common\model\HuiYuanSupervise;
use app\common\model\SystemSet;
use app\common\logic\DaiLi;
class HySuper extends Model
{
	protected $member_model;
	protected $sysmet;
	protected function initialize(){
		parent::initialize();
		$this->member_model = new HuiYuanSupervise();
		$this->sysmet = new SystemSet();
	}
	/**
	 * 会员列表
	 */
	public function listHys($limit)
	{
		//条件搜索
		$w = '';
		if(input('post.ss_sql')){
		    $w = "a.username LIKE '%".input('post.ss_sql')."%' or a.bankren LIKE '%".input('post.ss_sql')."%'";
		}
		if(input('sDate1') && input('sDate2')){
		    $star = strtotime(input('sDate1')." 00:00:00");
		    $end = strtotime(input('sDate2')." 23:59:59");
		    $where['a.reg_time']=['between',$star.','.$end];
		}
		
		if(input('get.login_ip')){
		    $where['a.login_ip'] = input('get.login_ip');
		}
		if(input('get.reg_ip')){
		    $where['a.reg_ip'] = input('get.reg_ip');
		}
		if(input('get.us_class')){
		    $where['a.us_class'] = input('get.us_class');
		}
		if(input('get.tjr')){
		    $where['a.tjr'] = input('get.tjr');
		}
		
		//选择条件查询
		$us = input('param.us');
		if($us == 1){
			$where['bankname']=['>',0];
		}elseif($us == 2){
			$where['czzje']=['>',0];
		}elseif($us == 3){
			$where['txzje']=['>',0];
		}elseif($us == 4){
			$where['tzzje']=['>',0];
		}elseif($us == 5){
			$where['je']=['>',0];
		}elseif($us == 6){
			$where['open']=['>',0];
		}elseif($us == 7){
		    $where['open']=['eq',0];
		}
		
		$where['a.swzh']=['<',1];
		$where['a.nbzh']=['<',1];

		return $this->member_model->memberList($where,$w,$limit);
	}
	/**
	 * 代理列表
	 */
	public function dlListHys($limit)
	{
		//条件搜索
		$a='';
		
			$wherea = array_filter(input('post.'))?array_filter(input('post.')):array_filter(input('get.'));
			$where = array();
			foreach ($wherea as $k=>$v){
				if($k == 'ss_sql'){
					$a=$wherea[$k];
				}
				if($k == 'sDate1' || $k == 'sDate2'){
				    if(isset($wherea['sDate1']) && isset($wherea['sDate2']) && $wherea['sDate1'] != '' && $wherea['sDate2'] !=''){
						$star = strtotime($wherea['sDate1']." 00:00:00");
						$end = strtotime($wherea['sDate2']." 23:59:59");
						$where['a.reg_time']=['between',$star.','.$end];
					}else{
						$this->sysmet->alertWin('选择完整的时间！', "/admin/Member/us_dllist");
					}
				}
			}
		
		
		//选择条件查询
		$us = input('param.us');
		if($us == 1){
			$where['a.bankname']=['>',0];
		}elseif($us == 2){
			$where['a.nbzh']=1;
		}elseif($us == 3){
			$where['a.txzje']=['>',0];
		}elseif($us == 4){
			$where['a.tzzje']=['>',0];
		}elseif($us == 5){
			$where['a.je']=['>',0];
		}elseif($us == 6){
			$where['a.open']=['>',0];
		}
		$where['a.swzh']=['<>','1'];
		
		
		
		return HySuper::name('username')
				->alias('a')
				->field(['a.id as uid','a.tjm as tjm','a.fandian as fandian','a.username as username','a.je as je','a.xjrens as xjrens','a.open as open','a.reg_time as reg_time','a.login_time as login_time','a.login_ip as login_ip','a.czzje as czzje','a.txzje as txzje','a.tzzje as tzzje','a.tjr as tjr','a.beizhu as beizhu','a.nbzh as nbzh','a.reg_ip as reg_ip','a.bankname as bankname'])
				->join('__US_CLASS__ b','b.id=a.us_class')
				->field(['b.class_name as hy_name'])
				->where($where)
				->where('a.username|a.id|a.bankren|a.mobile','like','%'.$a.'%')
				->order('a.id desc')
				->paginate($limit,false,['query'=>request()->param()]);

				
	}
//	['page'=>1],['ss_sql'=>input('ss_sql')],['sDate1'=>input('sDate1')],['sDate2'=>input('sDate2')]
	function sztrw($arr,$pid){
		static $arrs= array('czzje'=>0,'tzzje'=>0,'txzje'=>0);
		if(is_array($arr)){
			foreach ($arr as &$v){
	
				if($v['tjrid']==$pid){
	
					$arrs['czzje'] += $v['czzje'];
					$arrs['tzzje'] += $v['tzzje'];
					$arrs['txzje'] += $v['txzje'];
	
					$this->sztrw($arr,$v['id']);
				}
			}
		}
		return $arrs;
	}
	/**
	 * 查找相关信息
	 */
	public function CheckingRelevantInformation($id)
	{
		$query = HySuper::name('username')->where('id',$id)->find();
		return json(array($query['username'],$query['login_ip']));
	}
	
	/**
	 * 删除用户
	 * @param unknown $uid
	 * @
	 */
	public function delMember($uid){
		$where = array('id'=>$uid);
		$user = HySuper::name('username')->where($where)->field('username,nbzh,dl')->find();
		$name = HySuper::name('username')->where('tjr',$user['username'])->update(array('tjr'=>'','tjrid'=>0));
		HySuper::name('touzhu_list_no')->where('username',$user['username'])->delete();
		HySuper::name('username')->where($where)->update(array('sales'=>1));
		return $this->sysmet->alertWin('销户成功', "/admin/member/us_list?us=".input('us'));
	}
	/**
	 * 冻结用户状态
	 * @param unknown $uid
	 * @param unknown $open
	 */
	public function saveOpne($uid,$open){
		$where = array('id'=>$uid);
		$data['open']=$open;
		$user = HySuper::name('username')->where($where)->value('username');
		if($open==0){
			
			HySuper::name('weigui_jl')->where('username',$user)->update(array('status'=>1));
			$this->member_model->caozuorizhi_xieru('解冻账户:'.$user);
		}else{
			$this->member_model->caozuorizhi_xieru('冻结账户:'.$user);
		}
		$this->member_model->SaveUpdate('username', $where, $data);
		
		return $this->sysmet->alertWin('操作成功', "/admin/member/us_list");
	
	}
	public function nbsaveOpne($uid,$open){
		$where = array('id'=>$uid);
		$data['open']=$open;
		$user = HySuper::name('username')->where($where)->value('username');
		if($open==0){
			
			HySuper::name('weigui_jl')->where('username',$user)->update(array('status'=>1));
			$this->member_model->caozuorizhi_xieru('解冻内部账户:'.$user);
		}else{
			$this->member_model->caozuorizhi_xieru('冻结内部账户:'.$user);
		}
		$this->member_model->SaveUpdate('username', $where, $data);
		
		return $this->sysmet->alertWin('操作成功', "/admin/member/nbus_list");
	
	}
	/**
	 * 时间日期转换
	 * @param unknown $time
	 */
	public function Ymd($time){
		$first = date('Y-m-01',strtotime($time));
		$endtime = date('Y-m-d',strtotime("$first+1 month-1 day"));
		$start = strtotime($first."00:00:00");
		$end = strtotime($endtime."23:59:59");
		return $start.'|'.$end;
	}

	/**代理列表页查询**/
	public function dlList($limit){
		$times='';$a='';
		if(input('post.')){
				$wherea = input('post.');
				foreach ($wherea as $k=>$v){
					if($k == 'user_ss'){
						$a=$wherea[$k];
					}
					if($k=='date_ss')$times=$wherea[$k];
				}
			}
		
		$query = HySuper::name('daili')->where('username|id|dl_name|mobile','like','%'.$a.'%')->paginate($limit);
		$arr=array();
		$daili_logic = new DaiLi(); //代理logic
		$daili_renshu = array();
		foreach($query as $k=>$v){
			$where1['tjr']=$v['username'];
			//$q=HySuper::name('username')->where($where1)->select();
			//foreach($q as $val){
				//时间合集
				if($times=='')$times=date('Y-m');
				$cx_time = $this->Ymd($times);
				$cont_time = explode('|', $cx_time);

				$daili_renshu[$k]= $daili_logic->daili_rencont($v['username'],$cx_time);

			//}
			                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
			$arr[]=$query[$k];
			$arr['ren']=count($query);
		}
		return array('query'=>$query,'daili_renshu'=>$daili_renshu);
	}

	/**
	 ** 线下代理查询
	 ** @param  $dluser[string 代理人]
	 ** @param  $user_ss [string  人员搜索]
	 ** @param 	$date_ss[string 日期搜索]
	 ** @param 	$limit [int 查询条数]
	 ** return array
	**/

	public function dlListSingle($limit,$dluser)
	{
		$times=$a='';
		if(input('post.'))
		{
			$wherea = input('post.');
			foreach ($wherea as $k=>$v)
			{
				if($k == 'user_ss')
				{
					$a=$wherea[$k];
				}
				$k=='date_ss' && $times=$wherea[$k];
			}
		}
		
		$query = HySuper::name('username')->where("tjr='$dluser'")->where('username|id|bankren|mobile','like','%'.$a.'%')->paginate($limit,false,['query'=>['id'=>request()->get('id'),'cid'=>request()->get('cid')]]);

		$arr=array();
		$daili_logic = new DaiLi(); //代理logic
		$daili_renshu = array();
		foreach($query as $k=>$v){
			$where1['tjr']=$v['username'];
				//时间合集
				if($times=='')$times=date('Y-m');
				$cx_time = $this->Ymd($times);
				$daili_renshu[$k]= $daili_logic->user_rencont($v['username'],$cx_time);                                               
				$arr[]=$query[$k];
				$arr['ren']=count($query);
		}
		return array('query'=>$query,'daili_renshu'=>$daili_renshu);
	}

	
}